<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html dir="ltr">
<head>
  <title>C++ scripts</title>
  <meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
  <xml></xml>
  <link rel="stylesheet" type="text/css" href="MSDN.css">
</head>
<body id="divID" class="dtBODY">
<div id="nsbanner">
<div id="bannerrow1">
<table class="bannerparthead" cellspacing="0">
  <tbody>
    <tr id="hdr">
      <td class="runninghead">
      <p align="left">CS-Script 3.27.0</p>
      </td>
      <td class="product"> </td>
    </tr>
  </tbody>
</table>
</div>
<div id="TitleRow">
<h1 class="dtH1" align="left">C++ scripts</h1>
</div>
</div>
<br>
<div id="nstext"><font face="Arial" size="2"><span></span></font>Among
other programming languages CS-Script supports C++/CLI. <br>
<br>
<span style="font-weight: bold;"></span>The C++ script compiler is implemented as a pluggable custom compiler (see <a href="Alternative_Compilers.html">Alternative compilers</a>). C++ script compiler assembly is deployed and enabled at the time of the CS-Script installation/upgrade.<br>
<br>
<div style="margin-left: 40px;"><span style="font-style: italic;">Though VisualStudio, VisualStudio Express C++ or Windows SDK must also be installed in order to run C++ scripts.</span><br>
</div>
<br>
The most intriguing quality of C++/CLI syntax is that it may contain
calls to both managed and unmanaged libraries. The following code
illustrates just this:<br>
<font face="Arial" size="2"><span></span></font><font face="Arial" size="2"><span><br>
</span></font><font face="Arial" size="2"><span></span></font><font face="Arial" size="2"><span></span></font>
<table style="width: 710px; text-align: left;" border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td style="white-space: nowrap; background-color: rgb(255, 255, 204);"><font face="Courier New" size="2">MessageBoxA(0,&nbsp;<font color="#800000" size="2">"Hello&nbsp;World!&nbsp;(C++)"</font>,&nbsp;<font color="#800000" size="2">"Non-Managed"</font>,&nbsp;0);<br>
MessageBox::Show(<font color="#800000" size="2">"Hello&nbsp;World!&nbsp;(C++)"</font>,&nbsp;<font color="#800000" size="2">"Managed"</font>);</font></td>
    </tr>
  </tbody>
</table>
<font face="Arial" size="2"><span></span></font><br>
The code above makes use of managed <span style="color: rgb(0, 0, 102);">MessageBox.Show()</span> and unmanaged
<span style="color: rgb(0, 0, 102);">MessageBoxA()</span>. All this imply that C++/CLI script needs to be able to
"tell" the script engine what unmanaged libraries it depends on. This
can be done directly from code by using standard C++ linker directive.
The following code indicates that User32.lib static library is required
for running the script:<br>
<font face="Arial" size="2"><span><br>
</span></font><font face="Arial" size="2"><span></span></font>
<table style="width: 710px; text-align: left;" border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td style="white-space: nowrap; background-color: rgb(255, 255, 204);"><font face="Courier New" size="2"><font color="#0000ff" size="2">#pragma</font>&nbsp;comment(lib,&nbsp;<font color="#800000" size="2">"User32.lib"</font>)&nbsp;&nbsp;</font></td>
    </tr>
  </tbody>
</table>
<br>
The script code below is an example of the complete C++ script. It is the Hello.cpp script from the samples library (see <font face="Arial" size="2"><span></span></font><a href="Script_library.html">Script Library</a>).<font face="Arial" size="2"><span><br>
<br>
</span></font>
<table style="width: 710px; text-align: left;" border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td style="white-space: nowrap; background-color: rgb(255, 255, 204);"><font face="Courier New" size="2"><font color="#008000" size="2">//css_ref&nbsp;System.Windows.Forms;<br>
      </font></font><font face="Courier New" size="2"><font color="#0000ff" size="2">#pragma</font>&nbsp;comment(lib,&nbsp;<font color="#800000" size="2">"User32.lib"</font>)<br>
      <br>
      <font color="#0000ff" size="2">#include</font>&nbsp;<font color="#800000" size="2">"windows.h"</font><br>
      <font color="#0000ff" size="2">using</font>&nbsp;<font color="#0000ff" size="2">namespace</font>&nbsp;System;<br>
      <font color="#0000ff" size="2">using</font>&nbsp;<font color="#0000ff" size="2">namespace</font>&nbsp;System::Windows::Forms;<br>
      <br>
      <font color="#0000ff" size="2">public</font>&nbsp;ref&nbsp;<font color="#0000ff" size="2">class</font>&nbsp;Script<br>
{<br>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">public</font>:&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff" size="2">static</font>&nbsp;<font color="#0000ff" size="2">void</font>&nbsp;Main()<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console::WriteLine(L<font color="#800000" size="2">"Hello&nbsp;World!&nbsp;(C++)"</font>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MessageBoxA(0,&nbsp;<font color="#800000" size="2">"Hello&nbsp;World!&nbsp;(C++)"</font>,&nbsp;<font color="#800000" size="2">"Non-Managed"</font>,&nbsp;0);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MessageBox::Show(<font color="#800000" size="2">"Hello&nbsp;World!&nbsp;(C++)"</font>,&nbsp;<font color="#800000" size="2">"Managed"</font>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>
};</font></td>
    </tr>
  </tbody>
</table>
<font face="Arial" size="2"><span></span></font><br>
<font face="Arial" size="2"><span><span style="font-weight: bold;"></span></span></font><span style="font-weight: bold; font-style: italic;"></span><span style="font-weight: bold;">Limitations</span><br>
C++/CLI custom compiler relays on usage of MSBuilder.exe - language
independent building utility provided by Microsoft. MSBuild is a bit
slower than naked language specific compilers (e.g. csc.exe, cl.exe).
Also C++ compilation is slower by it's nature than compilation of C#
code. That is why you may notice that startup delay for C++ scripts is
slightly longer than for the other languages. However it is the case
only for the very first execution of the script.&nbsp; The script
engine uses cached execution data for any consequent executions of the
same unchanged script thus it will be no any excessive startup delay.
Just as with any scripts written in other languages.<font face="Arial" size="2"><span></span></font><br>
<h4 class="dtH4">See Also</h4>
<a href="Alternative_Compilers.html">Alternative
compilers</a> |&nbsp;<a href="Script_library.html">Script
Library</a><br>
<br>
<a href="ws.html"><span style="text-decoration: underline;"></span></a></div>
</body>
</html>

